/**
 * 
 */
package com.hector.meinewerk.client.view.widget.adapter;

import android.app.Service;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hector.meinewerk.client.R;
import com.hector.meinewerk.utils.ImageLoader;

public class GalleryAdapter extends BaseAdapter {

	private Context mContext;
	private String []mArrayUrl;
	private ImageLoader mImageLoader;

	public GalleryAdapter(Context context) {
		mContext = context;
		mImageLoader = new ImageLoader(mContext);
	}

	public void setArrayUrl(String [] array) {
		if (array == null)
			return;

		mArrayUrl = array;
	}

	@Override
	public int getCount() {
		if(mArrayUrl == null)
			return 0;
		return mArrayUrl.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		if(mArrayUrl == null)
			return null;
		return mArrayUrl[position];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	static class ViewHolder {
		ImageView imageView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater li = (LayoutInflater) mContext
					.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
			convertView = li.inflate(R.layout.item_image, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.image_view);

			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		mImageLoader.DisplayImage(mArrayUrl[position], holder.imageView);
		
		return convertView;
	}

}
